// packageA/pages/smart-home/index.js
const App = getApp();
let dictList = JSON.parse(JSON.stringify(App.globalData.dictTree));
const WXAPI = require("../../api/goods");
const baseUrl = require("../../../api/config").dev; //这里用的是ES6的写法
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isFromHomeSearch:'0',
    imgBaseUrl: `${baseUrl["imgBaseUrl"]}`,
    currentPageNum: 1,
    form: {
      pageNum: 1,
      pageSize: 20,
      type: "0",
      price: "0",
      sName: "",
    },
    isShow: false,
    listIsOver: false,
    isLoading: false,
    option1: [],
    option2: [],
    list: [],
    crossAxisCount: 2,
    crossAxisGap: 10,
    mainAxisGap: 10,
  },
  toBuyService(e){
    wx.navigateTo({
      url: `/packageA/pages/smart-detail/index?id=${e.currentTarget.dataset.id}&title=${e.currentTarget.dataset.title}`,
    })
  },
  changeOptions1(e) {
    this.setData({
      "form.type": e.detail,
    });
    this.getList()
  },
  changeOptions2(e) {
    this.setData({
      "form.price": e.detail,
    });
    this.getList()
  },
  search(e) {
    this.setData({
      form: {
        ...this.data.form,
        sName: e.detail.keyWords,
      },
    });
    this.getList();
  },
  openFilter() {
    this.setData({
      isShow: !this.data.isShow,
    });
  },
  formatOptions(arr) {
    let newArr = [
      {
        text: "全部",
        value: "0",
      },
    ];
    arr.map((item) => {
      newArr.push({
        text: item.sName,
        value: item.sValue,
      });
    });
    return newArr;
  },
  async getList() {
    this.setData({
      listIsOver:false,
      isLoading: true,
    });
    console.log("this.data", this.data);
    let params = {
      PageIndex: this.data.form.pageNum,
      PageSize: this.data.form.pageSize,
      sName: this.data.form.sName,
      dMinPrice:
        this.data.form.price == "0" ? 0 : this.data.form.price.split("-")[0],
      dMaxPrice:
        this.data.form.price == "0"
          ? 999999
          : (this.data.form.price.split("-")[1] || ''),
      iAduitState: 3,
      iClassification: 2,
      sTypeCode: this.data.form.type == 0 ? "" : this.data.form.type,
    };
    let isCustomLoading = true;
    let res = await WXAPI.getGoodsList(params, isCustomLoading);
    console.log(res);
    let listArr = [];
    if (res.data.code == 0) {
      if (this.data.form.pageNum == 1) {
        listArr = res.data.data.list;
        if (listArr && listArr.length > 0) {
          for (let i = 0; i < listArr.length; i++) {
            listArr[i].imgUrl = this.data.imgBaseUrl + listArr[i].sGoodsImgUrl;
          }
        }
      } else {
        listArr = this.data.serviceList;
        for (let i = 0; i < res.data.data.list.length; i++) {
          listArr[i].imgUrl =
            this.data.imgBaseUrl + res.data.data.list[i].sGoodsImgUrl;
        }
        Array.prototype.push.apply(listArr, res.data.data.list);
      }

      // if (res.data.data.list.length == 0) {
        this.setData({
          listIsOver: true,
        });
      // }

      this.setData({
        list: listArr,
        triggered: false,
        currentPageNum: res.data.data.list.length,
        isLoading: false,
      });
    }
  },
  onClickHide(){
    this.setData({
      isShow: false
    })
  },
  //上拉触底
  onScrollBottom() {
    if (this.data.form.pageNum < this.data.currentPageNum) {
      this.setData({
        "form.pageNum": this.data.pageNum + 1,
      });
      this.getList();
    } else if (
      this.data.form.pageNum > this.data.currentPageNum ||
      this.data.form.pageNum == this.data.currentPageNum
    ) {
      this.setData({
        listIsOver: true,
      });
    }
  },
  //下拉刷新
  async onRefresh() {
    this.setData({
      "form.pageNum": 1,
      list: [],
    });
    this.getList();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.keyWords) {
      this.setData({
        'form.sName':options.keyWords
      })
    }
    if(options.isFromHomeSearch){
      this.setData({
        isFromHomeSearch:options.isFromHomeSearch
      })
    }
    // 初始化筛选项下拉数据格式
    this.setData({
      option1: this.formatOptions(
        dictList.filter((item) => item.sValue == "PubIntelligenceDevice")[0]
          .Child
      ),
      option2: this.formatOptions(
        dictList.filter((item) => item.sValue == "PubIntelligenceDeviceFee")[0]
          .Child
      ),
    });
    this.getList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    if (this.data.isFromHomeSearch == "1") {
      let pages = getCurrentPages(); //获取当前页面pages里的所有信息。
      let prevPage = pages[pages.length - 2]; //prevPage 是获取上一个页面的js里面的pages的所有信息。 -2 是上一个页面，-3是上上个页面以此类推。
      prevPage.setData({
        // 将我们想要传递的参数在这里直接setData。上个页面就会执行这里的操作。
        searchValue: this.data.form.sName,
      });
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      "form.pageNum": 1,
      list: [],
    });
    this.getList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
